package org.example;

/**
 * Hello world!
 */

import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.client.RestTemplate;

import javax.validation.Validator;


@SpringBootApplication
public class App {

//    @Bean
//    public Validator validator() {
//        return new LocalValidatorFactoryBean();
//    }

    @Bean
    public RestTemplate buildPoolingRestTemplate(RestTemplateBuilder builder) {
        PoolingHttpClientConnectionManager connectionPoolManager = new PoolingHttpClientConnectionManager();
        connectionPoolManager.setMaxTotal(20);//最大连接数
        connectionPoolManager.setDefaultMaxPerRoute(2);//每个路由默认接收的最大连接数
        HttpClient httpClient = HttpClientBuilder.create().setConnectionManager(connectionPoolManager).build();
        HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
        httpRequestFactory.setHttpClient(httpClient);
        httpRequestFactory.setConnectionRequestTimeout(3000);//获取链接超时时间
        httpRequestFactory.setConnectTimeout(3000);// 指客户端和服务器建立连接的timeout
        httpRequestFactory.setReadTimeout(120000);// socketTimeout 读取数据的超时时间




        return builder.requestFactory(() -> httpRequestFactory).build();
    }

//    @Bean
//    public RestTemplate buildPoolingRestTemplate() {
//        return new RestTemplate();
//    }

    public static void main(String[] args) {

        String namesrvAddr = System.getenv("namesrv_addr");
        System.out.println("namesrvAddr = " + namesrvAddr);

        SpringApplication.run(App.class, args);
    }
}
